_word = "";
_kill = false;
_exist = false;
_focusedDisease = null;
_hide = true;
_listmousever = false;
_dsearchBoxFocused = false;

_autoComplete=function (){
	
	if(_kill) return;	

	if (_word != $("input[name=new_drug_name]").attr("value")) {

		_word = $("input[name=new_drug_name]").attr("value");
		_focusedDisease = null;
		$.ajax(
		{
			type: "GET",
			url: "ndd_drug_list.php",
			data: "key=" + encodeURI(_word),
			dataType: "xml",
			success: function(xml) {
				if ($("drug", xml).size() > 0)
				{
					$(".AutoCompleteDisease").remove();
					
					var _diseaseList = "";
					$("drug", xml).each(function(){
						_diseaseList += "<div class=\"AutoCompleteDisease\">"+ $(this).text() +"</div>\n";
					});
					$("#dsearch_auto_complete").append(_diseaseList);
					_hide = false;
					$(".AutoCompleteDisease").hover(_autoCompleteMouseOver);

				}
				else {
					_hide = true;
				}
			}
		});		
	}
	
	if( _word.length < 1 )
		_hide = true;

	
	if(_hide)
	{
		$("#dsearch_auto_complete").hide();
	}
	else
	{
		$("#dsearch_auto_complete").show();
	}
	
	setTimeout(_autoComplete, 10);
}
_autoCompleteClick = function()
{
	if(_focusedDisease!=null && _focusedDisease.html() != null) {
		_word = _focusedDisease.html();
		$("input[name=new_drug_name]").attr("value", _word);		
		$("input[name=new_drug_name]").focus();
		_exist = false;
		_kill = true;
		$("#dsearch_auto_complete").hide();
	}
	
}
_autoCompleteMouseOver =function()
{
	if (_focusedDisease== null || _focusedDisease != $(this))
	{
		if (_focusedDisease !=null)
			_focusedDisease.removeClass("focusedDisease");
			
		$(this).addClass("focusedDisease");
		_focusedDisease = $(this);
	}
}
_autoCompleteMouseIn=function()
{
	_listmousever=true;
}
_autoCompleteMouseOut=function()
{
	_listousever= false;
	if(!_dsearchBoxFocused)
	{
		_exist = false;
		_kill = true;
		$("#dsearch_auto_complete").hide();
	}
}
_startAutoComplete = function () {
	_dsearchBoxFocused=true;
	if(!_exist) {
		_kill = false;
		_exist = true;
		_word = "";		
		_autoComplete();
	}
}

_killAutoComplete = function () {	
	
	_dsearchBoxFocused = false;
	if(!_listmousever)
	{
		_exist = false;
		_kill = true;
		$("#dsearch_auto_complete").hide();
	}
}

_keyboardMove = function (e) {
	if (_kill = true && _word != $("input[name=new_drug_name]").attr("value"))
		_startAutoComplete();
	
	if($("#dsearch_auto_complete").css("display") == "none") {
	
		
		return;
	}

	var _temporaryDisease;
	
	if(e.keyCode == 40) {
		
		if(_focusedDisease == null) {
		
			_temporaryDisease = $("#dsearch_auto_complete div:first-child");
		}
		else {
			_temporaryDisease = _focusedDisease.next();
		}
		
		if(_temporaryDisease.html() != null) {
			
			if(_focusedDisease != null)
				_focusedDisease.removeClass("focusedDisease");			
				
			_temporaryDisease.addClass("focusedDisease");
		
			_focusedDisease = _temporaryDisease;
		}
		
		
		
		if(_focusedDisease.html() != null) {
			_word = _focusedDisease.html();
			$("input[name=new_drug_name]").attr("value", _word);
		}
	}
	else if(e.keyCode == 38) {
	
		if(_focusedDisease == null) {
			$("#dsearch_auto_complete").hide();
		}
		else {
			_temporaryDisease = _focusedDisease.prev();
		}
		
		if(_temporaryDisease.html() != null) {
		
			_focusedDisease.removeClass("focusedDisease");
			_temporaryDisease.addClass("focusedDisease");			
		
			_focusedDisease = _temporaryDisease;
		}
			
		if(_focusedDisease.html() != null) {
			_word = _focusedDisease.html();
			$("input[name=new_drug_name]").attr("value", _word);
		}
	}
}

$(document).ready(function(){
	$("input[name=new_drug_name]").focus(_startAutoComplete);
	$("input[name=new_drug_name]").blur(_killAutoComplete);
	$("input[name=new_drug_name]").keydown(_keyboardMove);
	$("#dsearch_auto_complete").click(_autoCompleteClick);
	$("#dsearch_auto_complete").hover(_autoCompleteMouseIn,_autoCompleteMouseOut);
});
